*This SAS script is written for the manuscript "Do Nonfinancial Firms Use Financial Assets to Take Risk" (Chen and Duchin, RCFS, 2023)
*
*It is used to replicate the main results in the paper.
*It needs to use the sas data set, cashholding_list_maindata, in folder of data, and sas macros in the folder of macros 
*
*please change to your directory for running the code;
%let path=C:\Users\Nicholas\Dropbox\Research\Research projects\FastRiskShifting\RCFS_Codes;
libname wrdsown 'C:\Users\nicholas\Dropbox\Research\SAS\wrdData'; *my own data directory;

libname dataDir "&path\data"; 
*---standard macros--;
%include "&path\macros\macro stat.sas"; *statistics; 
%include "&path\macros\macro nwords.sas"; *counting the number of variables;
%include "&path\macros\macro winsorize.sas";
%include "&path\macros\macro make_dummies.sas";
%include "&path\macros\macro FEREG0.sas"; *fixed effect regression with firm fixed effect and cluster stderr adjustment. Need to add time fixed effect manually;
%include "&path\macros\macro FEREG_LSDV.sas"; *fixed effect regression with firm fixed effect and cluster stderr adjustment. Need to add time fixed effect manually;
%include "&path\macros\macro FEREG.sas"; *fixed effect regression with firm fixed effect and cluster stderr adjustment. Need to add time fixed effect manually;
%include "&path\macros\macro fm.sas"; *fixed effect regression with firm fixed effect and cluster stderr adjustment. Need to add time fixed effect manually;

*include macro riskratio_did: calculate the mean of each group and perform diff-in-diff;
%include "&path\macros\FastRiskShifting_macros.sas";

proc datasets library = work kill;
delete all; 
run;
quit;

data cashholding_list_all32; 
set dataDir.cashholding_list_maindata;
d2011 = (year = 2011); *year dummy variables;
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);
run;

data cashholding_list_all32; set dataDir.cashholding_list_maindata;
proc sort data = cashholding_list_all32; by cik year; run;

*----part 2.0.----------table 1. summary statistics-----;
%let controlvars = ttl_risk_fin_assets risk_ratio_fin mat2 booklev tang logsale profit mtob capinvest divratio ;
%let bdata = cashholding_list_all32; %let keyvar = ttl_safe_assets; %let svars = &keyvar &controlvars; %let bstats = cashholding_stats; %let PostInd = 0;
%basicData;

*Table 1;
Proc print data = cashholding_stats; run;

*Table 2 bankruptcy prediction, see the folder bankruptcy prediction that contains data and Stata codes;
* can not provide the UCLA-LoPucki Bankruptcy Research Data;

*------------------------------------------------------------------;
* All panels in Table 3 and one panel of Table 4d
*-------------------------------------------------------------------;

*key independent variable = lmat20ave;

*-- Table 3A;
%let baseyear = 2014;
%let rankvar0 = lmat20ave; *use lmat10ave and lmat30ave (Table 4B), or use scaled by total assets stloan (Table 4c);

%let controls0 = logsale profit mtob divratio capinvest booklev;
*this macro will take log of ttl_risk_fin_assets;
%riskratio_did33(inputdata = cashholding_list_all32, baseyear = &baseyear, rankvar = &rankvar0, riskvar = ttl_risk_fin_assets, 
out = cash_hstloan_didresults_log, outcontinu = cash_hstloan_didresultsc_log, outmean = cash_hstloan_mean_log, group_num = 3, rankind = 1, diff = 1, 
chars = , charmean = cash_hstloan_chars, charstd = cash_hstloan_std, controls = &controls0); *capinvest divratio eqissue debtissue;

%riskratio_did26(inputdata = cashholding_list_all32, baseyear = &baseyear, rankvar = &rankvar0, riskvar = risk_ratio_fin, 
out = cash_hstloan_didresults, outcontinu = cash_hstloan_didresultsc, outmean = cash_hstloan_mean, group_num = 3, rankind = 1, diff = 1, chars = , controls = &controls0);

*Table 3A results;
data cash_hstloan_didresults_all;
merge cash_hstloan_didresults_log(keep = parameter value3 rename = (value3 = C3)) 
cash_hstloan_didresults(keep = value3 rename = (value3 = D3));
if parameter in ('d2012','d2013', 'd2014','d2015', 'd2016') then delete;
run;

proc print data = cash_hstloan_didresults_all(where = (not missing(c3))); run;

*Table 4D, continuous ranking variables;
data cash_hstloan_didresults_allc;
merge cash_hstloan_didresultsc_log(keep = parameter value3 rename = (value3 = C3)) 
cash_hstloan_didresultsc(keep = value3 rename = (value3 = D3));
if parameter in ('d2012','d2013', 'd2014','d2015', 'd2016') then delete;
run;

proc print data = cash_hstloan_didresults_allc(where = (not missing(c3))); run;

*------------------ Table 3b;
%riskratio_did26(inputdata = cashholding_list_all32, baseyear = &baseyear, rankvar = &rankvar0, riskvar = risk_ratio_fin, 
out = cash_hstloan_didresults, outcontinu = cash_hstloan_didresultsc, outmean = cash_hstloan_mean, group_num = 3, rankind = 1, diff = 1, chars = , controls = &controls0);

data cashholding_list_all36;
set cashholding_list_all32;
hpred = (lmat20ave > 0.52898); *mat2:   0.33474    0.52898    0.60829    0.68111 ;

d2011 = (year = 2011); *year dummy variables;
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);

hd2011 = hpred*d2011;
hd2012 = hpred*d2012;
hd2013 = hpred*d2013;
hd2014 = hpred*d2014;
hd2015 = hpred*d2015;
hd2016 = hpred*d2016;

log_risk_fin_assets = log(1+ttl_risk_fin_assets);
run;

%let controlyrs = hd2011 hd2012 hd2014 hd2015 hd2016 &controls0 d2011 d2012 d2014 d2015 d2016;
%FEREG_LSDV(risk_ratio_fin, &controlyrs, cik, year, cik, cashholding_list_all36, inter_results);
%FEREG_LSDV(log_risk_fin_assets, &controlyrs, cik, year, cik, cashholding_list_all36, inter_results3);

data all_results;
merge inter_results3(keep = value rename = (value = log))  inter_results(keep = parameter value);
if parameter in ('Intercept', 'd2011', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
run;
proc print data = all_results; run;

*-------------------Table 3c;
*investment opportunities;
data cashholding_list_all32_2;
set cashholding_list_all32;
mtob_post = mtob*post;
risk_ratio_well_post = risk_ratio_well*post;

log_risk_well_assets_post = log_risk_well_assets*post;
all_well_ratio_post = all_well_ratio*post;

log_all_well_assets_post = log_all_well_assets*post;

run;

proc means data = cashholding_list_all32 n mean std min p25 p50 p75 max;
var all_well_ratio ttl_all_wells;
run;

%let baseyear = 2014;
%let rankvar0 = lmat20ave; 

%let controls0 = logsale profit divratio capinvest booklev mtob mtob_post log_all_well_assets log_risk_well_assets_post;  * lrisk_ratio_fin_at  aqc stdebtissue  ltdebtissue;

%riskratio_did33(inputdata = cashholding_list_all32_2, baseyear = &baseyear, rankvar = &rankvar0, riskvar = ttl_risk_fin_assets, 
out = cash_hstloan_didresults_log, outcontinu = cash_hstloan_didresultsc_log, outmean = cash_hstloan_mean_log, group_num = 3, rankind = 1, diff = 1, 
chars = , charmean = cash_hstloan_chars, charstd = cash_hstloan_std, controls = &controls0); *capinvest divratio eqissue debtissue;

%let controls0 = logsale profit divratio capinvest booklev mtob mtob_post all_well_ratio all_well_ratio_post; 
%riskratio_did26(inputdata = cashholding_list_all32_2, baseyear = &baseyear, rankvar = &rankvar0, riskvar = risk_ratio_fin, 
out = cash_hstloan_didresults, outcontinu = cash_hstloan_didresultsc, outmean = cash_hstloan_mean, group_num = 3, rankind = 1, diff = 1, chars = , controls = &controls0);

data cash_hstloan_didresults_all;
merge cash_hstloan_didresults_log(keep = parameter value3 rename = (value3 = C3)) 
cash_hstloan_didresults(keep = value3 rename = (value3 = D3));
if parameter in ('Intercept', 'd2011', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
run;

proc print data = cash_hstloan_didresults_all(where = (not missing(c3))); run;

*------------------------------------------------------------------;
* table 4
*-------------------------------------------------------------------;
*----------------------Table 4A-------------------------------------;
data cashholding_list_all38;
set cashholding_list_all32;
if 1300 <= sic_num <=1399;
run;

%let baseyear = 2014; 
%let rankvar0 = lmat20ave;

%let controls0 = logsale profit mtob divratio capinvest booklev;  * lrisk_ratio_fin_at  aqc stdebtissue  ltdebtissue;
%riskratio_did33(inputdata = cashholding_list_all38, baseyear = &baseyear, rankvar = &rankvar0, riskvar = ttl_risk_fin_assets, 
out = cash_hstloan_didresults_log, outcontinu = cash_hstloan_didresultsc_log, outmean = cash_hstloan_mean_log, group_num = 3, rankind = 1, diff = 1, 
chars = , charmean = cash_hstloan_chars, charstd = cash_hstloan_std, controls = &controls0); *capinvest divratio eqissue debtissue;

%riskratio_did26(inputdata = cashholding_list_all38, baseyear = &baseyear, rankvar = &rankvar0, riskvar = risk_ratio_fin, 
out = cash_hstloan_didresults, outcontinu = cash_hstloan_didresultsc, outmean = cash_hstloan_mean, group_num = 3, rankind = 1, diff = 1, chars = , controls = &controls0);

data cash_hstloan_didresults_all;
merge cash_hstloan_didresultsc_log(keep = parameter value3 rename = (value3 = C3)) 
cash_hstloan_didresultsc(keep = value3 rename = (value3 = D3));
if parameter in ('Intercept', 'var','post', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
run;

proc print data = cash_hstloan_didresults_all; run;

*-------------------Table 4B and Table 4C---;
%let baseyear = 2014;
%let rankvar0 = stloan1; *use lmat10ave and lmat30ave for Table 4B, or use stloan1 (ST debt scaled by total assets) for Table 4c;

%let controls0 = logsale profit mtob divratio capinvest booklev;
%riskratio_did33(inputdata = cashholding_list_all32, baseyear = &baseyear, rankvar = &rankvar0, riskvar = ttl_risk_fin_assets, 
out = cash_hstloan_didresults_log, outcontinu = cash_hstloan_didresultsc_log, outmean = cash_hstloan_mean_log, group_num = 3, rankind = 1, diff = 1, 
chars = , charmean = cash_hstloan_chars, charstd = cash_hstloan_std, controls = &controls0); *capinvest divratio eqissue debtissue;

%riskratio_did26(inputdata = cashholding_list_all32, baseyear = &baseyear, rankvar = &rankvar0, riskvar = risk_ratio_fin, 
out = cash_hstloan_didresults, outcontinu = cash_hstloan_didresultsc, outmean = cash_hstloan_mean, group_num = 3, rankind = 1, diff = 1, chars = , controls = &controls0);

*diff-in-diff;
data cash_hstloan_didresults_all;
merge cash_hstloan_didresults_log(keep = parameter value3 rename = (value3 = C3)) 
cash_hstloan_didresults(keep = value3 rename = (value3 = D3));
if parameter in ('Intercept', 'var','post', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
run;

proc print data = cash_hstloan_didresults_all(where = (not missing(c3))); run;

*------------- Table 4D -------------------------------------------------;
* Table 4D results are in the section of Table 3;

*------------------Table 4E-----------------------------------------------;
%let funds = mat2; *note this is not average mat2, as stated in table mat2 is maturity ratio in 2010, and 2013;
%let controls = logsale profit mtob divratio capinvest booklev; 

data cashholding_list_all33;
set cashholding_list_all32;
hpred = (lmat20ave > 0.33474); *mat2:   0.33474    0.52898    0.60829    0.68111 ;

debtissue_hpred = debtissue*hpred;
debtissue_hpred = debtissue*hpred;
debtissue3_hpred = debtissue3*hpred;
LTDebtIssue_hpred = LTDebtIssue*hpred;
STDebtIssue_hpred = STDebtIssue*hpred;
eqissue_hpred = eqissue*hpred;
cf_hpred = cf*hpred;
disinvest_hpred = disinvest*hpred;
capinvest_hpred = capinvest*hpred;
chgs_safe_fin_assets_at_hpred = chgs_safe_fin_assets_at*hpred;
profit_hpred = profit*hpred;

chgs_safe_finast_at_H = chgs_safe_fin_assets_at*hpred;

neglogba = logba;
run;

*change from 2011 to 2013;
data cashholding_list_all36(drop = year);
merge cashholding_list_all32(in = in1 keep = cik year &funds log_risk_fin_assets log_safe_fin_assets ttl_risk_fin_assets ttl_safe_assets risk_ratio_fin &controls where = (year = 2011)) 
cashholding_list_all32(keep = cik year log_risk_fin_assets log_safe_fin_assets ttl_risk_fin_assets ttl_safe_assets risk_ratio_fin where = (year = 2013)
rename = (log_risk_fin_assets=log_risk_fin_assets16 log_safe_fin_assets=log_safe_fin_assets16 risk_ratio_fin=risk_ratio_fin16 ttl_risk_fin_assets=ttl_risk_fin_assets16 ttl_safe_assets=ttl_safe_assets16));
chgs_risk_fin_assets = log_risk_fin_assets16 - log_risk_fin_assets;
chgs_safe_fin_assets = log_safe_fin_assets16 - log_safe_fin_assets;
chgs_risk_ratio_fin = risk_ratio_fin16 - risk_ratio_fin;
if in1;
by cik;
run;

%FEREG(chgs_safe_fin_assets, &funds &controls, cik, year, cik, cashholding_list_all36, chgs_fin_cond);
%FEREG(chgs_risk_fin_assets, &funds &controls, cik, year, cik, cashholding_list_all36, chgs_fin_cond1);
%FEREG(chgs_risk_ratio_fin, &funds &controls, cik, year, cik, cashholding_list_all36, chgs_fin_cond2);

data chgs_fin_cond;
set chgs_fin_cond;
retain nn 0;
nn = nn + 1;
run;

proc sort data = chgs_fin_cond; by parameter _type; run;
proc sort data = chgs_fin_cond1; by parameter _type; run;
proc sort data = chgs_fin_cond2; by parameter _type; run;

data cf_funds_cond;
merge chgs_fin_cond(rename = (value = chgs_fin))
chgs_fin_cond1(rename = (value = chgs_fin1))
chgs_fin_cond2(rename = (value = chgs_fin2));
if parameter in ('Intercept', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
by  parameter _type; 
run;

proc sort data = cf_funds_cond; by nn;
data cf_funds_cond; set cf_funds_cond; drop nn; run;

*print pre-crisis results, columns (1) and (3);
proc print data = cf_funds_cond(drop = chgs_fin); run;

*change from 2014 to 2016;
data cashholding_list_all36(drop = year);
merge cashholding_list_all32(in = in1 keep = cik year &funds log_risk_fin_assets log_safe_fin_assets ttl_risk_fin_assets ttl_safe_assets risk_ratio_fin &controls where = (year = 2014)) 
cashholding_list_all32(keep = cik year log_risk_fin_assets log_safe_fin_assets ttl_risk_fin_assets ttl_safe_assets risk_ratio_fin where = (year = 2016)
rename = (log_risk_fin_assets=log_risk_fin_assets16 log_safe_fin_assets=log_safe_fin_assets16 risk_ratio_fin=risk_ratio_fin16 ttl_risk_fin_assets=ttl_risk_fin_assets16 ttl_safe_assets=ttl_safe_assets16));
chgs_risk_fin_assets = log_risk_fin_assets16 - log_risk_fin_assets;
chgs_safe_fin_assets = log_safe_fin_assets16 - log_safe_fin_assets;
chgs_risk_ratio_fin = risk_ratio_fin16 - risk_ratio_fin;
if in1;
by cik;
run;

%FEREG(chgs_safe_fin_assets, &funds &controls, cik, year, cik, cashholding_list_all36, chgs_fin_cond);
%FEREG(chgs_risk_fin_assets, &funds &controls, cik, year, cik, cashholding_list_all36, chgs_fin_cond1);
%FEREG(chgs_risk_ratio_fin, &funds &controls, cik, year, cik, cashholding_list_all36, chgs_fin_cond2);

data chgs_fin_cond;
set chgs_fin_cond;
retain nn 0;
nn = nn + 1;
run;

proc sort data = chgs_fin_cond; by parameter _type; run;
proc sort data = chgs_fin_cond1; by parameter _type; run;
proc sort data = chgs_fin_cond2; by parameter _type; run;

data cf_funds_cond;
merge chgs_fin_cond(rename = (value = chgs_fin))
chgs_fin_cond1(rename = (value = chgs_fin1))
chgs_fin_cond2(rename = (value = chgs_fin2));
if parameter in ('Intercept', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
by  parameter _type; 
run;

proc sort data = cf_funds_cond; by nn;
data cf_funds_cond; set cf_funds_cond; drop nn; run;
*print post-crisis results, columns (2) and (4);
proc print data = cf_funds_cond(drop = chgs_fin); run;


*---------------start Table 9--------;
* table 9 panel regressions------;
*-----------------------funding the cost-------------------;

proc univariate data = cashholding_list_all32(where = (year = 2013)) noprint;
var lmat20ave;
output out=percentiles1 pctlpts=33 pctlpts=50 pctlpts=66.667 pctlpts=75 pctlpts=80 pctlpts=90 pctlpts=100 pctlpre=P;
proc print data=percentiles1; run;

data cashholding_list_all33;
set cashholding_list_all32;

d2011 = (year = 2011); *year dummy variables;
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);

hpred = (lmat20ave > 0.52898); *mat2:   0.33474    0.52898    0.60829    0.68111 ;
chgs_safe_fin_assets_at_hpred = chgs_safe_fin_assets_at*hpred;
run;

data cashholding_list_all331;
set cashholding_list_all33;
if 2011<=year <=2013; *before the crisis;
run;

data cashholding_list_all332;
set cashholding_list_all33;
if 2014<=year <=2016; *after the crisis;
run;
*-------------------------column (1 3) in Table 9;
%let funds = chgs_safe_fin_assets_at  cf debtIssue eqissue disinvest;
%let controls = logsale profit mtob divratio capinvest booklev; * lrisk_ratio_fin_at debtissue eqissue;

%let controlyrs = d2012 d2013 d2014 d2015 d2016;
%FEREG_LSDV(chgs_risk_fin_assets_at, &funds &controls &controlyrs, cik, year, cik, cashholding_list_all33, cf_funds_risk_cond);
%let controlyrs = d2012 d2013;
%FEREG_LSDV(chgs_risk_fin_assets_at, &funds &controls &controlyrs, cik, year, cik, cashholding_list_all331, cf_funds_risk_cond1);
%let controlyrs = d2015 d2016;
%FEREG_LSDV(chgs_risk_fin_assets_at, &funds &controls &controlyrs, cik, year, cik, cashholding_list_all332, cf_funds_risk_cond2);

data cf_funds_risk_cond;
set cf_funds_risk_cond;
retain nn 0;
nn = nn + 1;
run;

proc sort data = cf_funds_risk_cond; by parameter _type; run;
proc sort data = cf_funds_risk_cond1; by parameter _type; run;
proc sort data = cf_funds_risk_cond2; by parameter _type; run;

*cond1 and cond 2 are for column 1 and 3 of Table 8;
data cf_funds_cond;
merge cf_funds_risk_cond(rename = (value = cf_funds_risk))
cf_funds_risk_cond1(rename = (value = cf_funds_risk1))
cf_funds_risk_cond2(rename = (value = cf_funds_risk2));
if parameter in ('Intercept', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
by  parameter _type; 
run;

proc sort data = cf_funds_cond; by nn;
data cf_funds_cond; set cf_funds_cond; drop nn; run;
proc print data = cf_funds_cond(drop = cf_funds_risk); run; 

*-------------------------column (2, 4) in Table 9;
%let funds = chgs_safe_fin_assets_at chgs_safe_fin_assets_at_hpred cf debtIssue eqissue disinvest;
%let controls = logsale profit mtob divratio capinvest booklev; * lrisk_ratio_fin_at debtissue eqissue;

%let controlyrs = d2012 d2013 d2014 d2015 d2016;
%FEREG_LSDV(chgs_risk_fin_assets_at, &funds &controls &controlyrs, cik, year, cik, cashholding_list_all33, cf_funds_risk_cond);
%let controlyrs = d2012 d2013;
%FEREG_LSDV(chgs_risk_fin_assets_at, &funds &controls &controlyrs, cik, year, cik, cashholding_list_all331, cf_funds_risk_cond1);
%let controlyrs = d2015 d2016;
%FEREG_LSDV(chgs_risk_fin_assets_at, &funds &controls &controlyrs, cik, year, cik, cashholding_list_all332, cf_funds_risk_cond2);

data cf_funds_risk_cond;
set cf_funds_risk_cond;
retain nn 0;
nn = nn + 1;
run;

proc sort data = cf_funds_risk_cond; by parameter _type; run;
proc sort data = cf_funds_risk_cond1; by parameter _type; run;
proc sort data = cf_funds_risk_cond2; by parameter _type; run;

*cond1 and cond 2 are for column 2 and 4 of Table 8;
data cf_funds_cond;
merge cf_funds_risk_cond(rename = (value = cf_funds_risk))
cf_funds_risk_cond1(rename = (value = cf_funds_risk1))
cf_funds_risk_cond2(rename = (value = cf_funds_risk2));
if parameter in ('Intercept', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
by  parameter _type; 
run;

proc sort data = cf_funds_cond; by nn;
data cf_funds_cond; set cf_funds_cond; drop nn; run;
proc print data = cf_funds_cond(drop = cf_funds_risk); run; *column (2, 4) in Table 9;

